<?php
/**
 * This is Users controller file. 
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil.controllers
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
/**
 * UsersController class
 *
 * @uses          LilAppController
 * @package       lil
 * @subpackage    lil.controllers
 */
class UsersController extends LilAppController {
/**
 * name property
 *
 * @var string 'Users'
 * @access public
 */
	var $name = 'Users';
/**
 * uses property
 *
 * @var array
 * @access public
 */
	var $uses = array('Lil.User');
/**
 * components property
 *
 * @var array
 * @access public
 */
	var $components = array('Cookie', 'Session', 'Auth', 'Security');
/**
 * isAuthorized method
 *
 * @access public
 * @return bool
 */
	function isAuthorized() {
		if (!parent::isAuthorized()) return false;
		if (isset($this->Auth) && !Configure::read('Lil.allowUsersAnything')) {
			if (in_array($this->params['action'], array('admin_properties', 'logout'))) {
				return true;
			} else {
				return (bool)$this->Auth->user(Configure::read('Lil.userAdminField'));
			}
		}
		return true;
	}
/**
 * beforeFilter method
 *
 * @access public
 * @return void
 */
	function beforeFilter() {
		parent::beforeFilter();
		if (isset($this->Auth)) {
			$this->Auth->allowedActions = array('login', 'logout', 'reset', 'verify');
			$this->Auth->autoRedirect = false;
		}
	}
/**
 * login method
 *
 * @access public
 * @return void
 */
	function login() {
		// code inside this function will execute only when autoRedirect was set to false
		$useLayout = Configure::read('Lil.useAdminLayout');
		if ($useLayout) $this->layout = 'lil_admin';
		
		if (isset($this->Auth)) {
			if (empty($this->data)) {
				$cookie = $this->Cookie->read($this->Auth->sessionKey);
				if (!is_null($cookie)) {
					if ($this->Auth->login($cookie)) {	
						//  Clear auth message, just in case we use it.
						$this->Session->delete('Message.auth');
						$this->redirect($this->Auth->redirect());
					}
				}
			}
			if ($this->Auth->user()) {
				if (!empty($this->data[$this->Auth->userModel]['remember_me'])) {
					$f_username = $this->Auth->fields['username'];
					$f_pass = $this->Auth->fields['password'];
					
					$cookie = array();
					$cookie[$f_username] = $this->data[$this->Auth->userModel][$f_username];
					$cookie[$f_pass] = $this->data[$this->Auth->userModel][$f_pass];
					$this->Cookie->write($this->Auth->sessionKey, $cookie, true, '+2 weeks');
					unset($this->data[$this->Auth->userModel]['remember_me']);
				}
				$this->redirect($this->Auth->redirect());
			}
		}
	}
/**
 * logout method
 *
 * @access public
 * @return void
 */
	function logout() {
		if (isset($this->Auth)) {
			if ($this->Cookie->read($this->Auth->sessionKey)) {
				$this->Cookie->delete($this->Auth->sessionKey);
			}
			$this->Auth->logout();
			$this->Session->setFlash(__d('lil', 'You\'ve been logged out.', true));
			$this->redirect($this->Auth->logoutRedirect);
		} else {
			$this->redirect('/');
		}
	}
/**
 * admin_index method
 *
 * @access public
 * @return void
 */
	function admin_index() {
		$this->User->recursive = -1;
		$this->set('data', $this->paginate('User'));
	}
/**
 * admin_add method
 *
 * @access public
 * @return void
 */
	function admin_add() {
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__d('lil', 'A new user has been added.', true));
				$this->redirect(array('admin' => true, 'plugin' => 'lil', 'controller' => 'users', 'action' => 'index'));
			} else {
				$this->Session->setFlash(__d('lil', 'Please verify that the information is correct.', true), 'error');
			}
		}
		
		if (!$areas = Configure::read('Lil.noAreas')) {
			$Area = ClassRegistry::init('Lil.Area');
			$this->set('areas', $Area->find('list'));
		}
	}
/**
 * admin_edit method
 *
 * @param array $id
 * @access public
 * @return void
 */
	function admin_edit($id = null) {
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__d('lil', 'User has been saved.', true));
				$this->redirect(array('admin' => true, 'plugin' => 'lil', 'controller' => 'users', 'action' => 'index'));
			} else {
				$this->Session->setFlash(__d('lil', 'Please verify that the information is correct.', true), 'error');
			}
		} else if ($this->data = $this->User->find(
			'first', 
			array(
				'conditions' => array('User.id' => $id),
				'contain'    => Configure::read('Lil.noAreas') ? null : 'Area',
				'recursive'  => 1
			)
		)) {
			unset($this->data['User']['passwd']);
		} else if (!is_null($id)) {
			$this->error404();
		}
		
		if (!$areas = Configure::read('Lil.noAreas')) {
			$Area = ClassRegistry::init('Lil.Area');
			$this->set('areas', $Area->find('list'));
		}
	}
/**
 * admin_delete method
 *
 * @param array $id
 * @access public
 * @return void
 */
	function admin_delete($id=null) {
		if (is_numeric($id) && $data = $this->User->findById($id)) {
			$this->User->delete($id);
			$this->Session->setFlash(__d('lil', 'User has been deleted.', true));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->error404();
		}
	}
/**
 * admin_properties method
 *
 * @param array $id
 * @access public
 * @return void
 */
	function admin_properties() {
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__d('lil', 'Properties have been saved.', true));
				$this->redirect(Router::url(null, true));
			} else {
				$this->Session->setFlash(__d('lil', 'Please verify that the information is correct.', true), 'error');
			}
		} else if (($id = $this->Auth->user('id')) && $this->data = $this->User->read(null, $id)) {
			unset($this->data['User']['passwd']);
		} else if (!is_numeric($id)) {
			$this->error404();
		}
	}
}